package ink.lovejinhu.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jinhu
 * created 2021-06-28 8:35
 */
public class Solution_0119 {
    public List<List<Integer>> outer = new ArrayList<>();

    /**
     * 杨辉三角2，只返回第n行的数据
     *
     * @param rowIndex
     * @return
     */
    public List<Integer> getRow(int rowIndex) {

        List<Integer> inner1 = new ArrayList<>();
        List<Integer> inner2 = new ArrayList<>();
        inner1.add(1);
        inner2.add(1);
        inner2.add(1);
        outer.add(inner1);
        outer.add(inner2);
        if (rowIndex <= 1) {
            return outer.get(rowIndex);
        }
        for (int i = 2; i <=rowIndex ; i++) {
            List<Integer> temp = new ArrayList<>();
            temp.add(1);
            for (int j = 0; j < outer.get(i-1).size()-1; j++) {
                int i1 = outer.get(i - 1).get(j) + outer.get(i - 1).get(j + 1);
                temp.add(i1);
            }
            temp.add(1);
            outer.add(temp);
        }

        return outer.get(rowIndex);
    }
    public List<Integer> getRow1(int rowIndex) {
        Integer[] dp = new Integer[rowIndex + 1];
        Arrays.fill(dp,1);
        for(int i = 2;i < dp.length;i++){
            for(int j = i - 1;j > 0;j--)
                dp[j] = dp[j] + dp[j - 1];
        }
        List<Integer> res = Arrays.asList(dp);
        return res;
    }
    public static void main(String[] args) {
        Solution_0119 solution_0119 = new Solution_0119();
        List<Integer> row = solution_0119.getRow(3);
        System.out.println(row);
        List<Integer> row1 = solution_0119.getRow1(3);
        System.out.println(row1);
    }
}
